package com.magiccompass.alipay.barcode.security.service;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magiccompass.alipay.barcode.entity.UserInfo;
import com.magiccompass.alipay.barcode.security.model.LoginUserModel;
import com.magiccompass.alipay.barcode.service.UserService;

@Service("loginService")
@Transactional
public class LoginService implements UserDetailsService {
	@Resource(name="userService")
	private UserService userService;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		try {
			UserInfo operator = userService.findUserInfoByUserName(username);
			
			if(operator == null){
				throw new UsernameNotFoundException(username+" not exist.");
			}else{
				UserDetails returnUserDetails = new LoginUserModel(
						operator.getUserName(), 
						operator.getPassword(),
						operator.getId(),
						operator);
				return 	returnUserDetails;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
